package cn.shengchao.examstar.ai.acl.adapters;

import cn.shengchao.examstar.shared.acl.ai.AIKnowledgeEvaluationService;
import cn.shengchao.examstar.shared.acl.ai.dto.KnowledgeEvaluationResult;
import cn.shengchao.examstar.ai.infrastructure.external.ai.service.DifyChatflowClientService;
import cn.shengchao.examstar.ai.infrastructure.external.ai.service.res.KnowledgePointMasteryEvaluationDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;
import java.util.ArrayList;
import lombok.extern.slf4j.Slf4j;

/**
 * AI知识点评估服务实现
 * 适配器模式：将内部AI服务转换为共享内核中定义的接口
 */
@Slf4j
@Service
public class AIKnowledgeEvaluationServiceImpl implements AIKnowledgeEvaluationService {

    @Autowired
    private DifyChatflowClientService difyChatflowClientService;
    
    @Override
    public KnowledgeEvaluationResult evaluateKnowledgeMastery(Long userId, String exerciseData) throws IOException {
        log.info("调用AI服务评估知识点掌握度 - 用户ID: {}", userId);
        
        // 调用原有服务
        KnowledgePointMasteryEvaluationDTO evaluation = 
                difyChatflowClientService.executeKnowledgeMasteryEvaluationWorkflow(userId, exerciseData);
        
        // 转换为共享DTO
        KnowledgeEvaluationResult result = new KnowledgeEvaluationResult();
        result.setSummary(evaluation.getSummary());
        
        // 转换知识点评估项
        List<KnowledgeEvaluationResult.KnowledgePointEvaluation> items = new ArrayList<>();
        
        if (evaluation.getKnowledgePointMasteries() != null) {
            for (KnowledgePointMasteryEvaluationDTO.KnowledgePointMasteryItem item : evaluation.getKnowledgePointMasteries()) {
                KnowledgeEvaluationResult.KnowledgePointEvaluation eval = 
                        new KnowledgeEvaluationResult.KnowledgePointEvaluation();
                eval.setKnowledgePointId(item.getKnowledgePointId());
                eval.setDegreeOfMastery(item.getDegreeOfMastery());
                eval.setAccuracyRate(item.getAccuracyRate());
                eval.setAnalysis(item.getAnalysis());
                items.add(eval);
            }
        }
        
        result.setKnowledgePointEvaluations(items);
        log.info("AI服务评估完成 - 用户ID: {}, 知识点数量: {}", userId, items.size());
        
        return result;
    }
} 